﻿/*
	Copyright 2008 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
/*
	Directories
*/
/***********************************************************************
  Add a directory
***********************************************************************/
function addDir(){
	var dir = "";

	var objShell = new ActiveXObject("Shell.Application")
	var objF = objShell.BrowseForFolder (0, SELECT_DIR, 17, "C:\\" )

	if( objF )
	{
		var folderItem = objF.ParentFolder.ParseName( objF.Title );
		dir = folderItem.Path;

		var dirs = restoreDirs()
		dirs.push( dir  )
		saveDirs( dirs );
	}	
	showListbox();
}

/***********************************************************************
  Restore directories
***********************************************************************/
function restoreDirs(){
	var dirs = new Array();
	var store = options.getValue( "dirs" );
	var f = store.split("@@@" );
	for( var i = 1; i < f.length; i ++)		// Skip first empty element
		dirs.push( f[i] );

	return dirs;
}

/***********************************************************************
 Save Directories to option Values
***********************************************************************/
function saveDirs( dirs ){
	var	store = ""

	for( var i = 0; i<dirs.length; i++)
		store = store + "@@@" + dirs[i];
	options.putValue( "dirs", store );
}


/***********************************************************************
  Load files
***********************************************************************/
function loadShortcuts( dirs ){
	var empty = new Array();
	var folder, fc;
	var file, iconFile;

	shortcuts = empty;

	// Get Information for all files in directory
	for( var i = 0; i< dirs.length;i++){
		folder =  framework.system.filesystem.GetFolder( dirs[i] )
		fc = new Enumerator( folder.files );
		
		// Parse Files
		for ( ; !fc.atEnd(); fc.moveNext() ) {
			file = fc.item();

			var shortcut = new Object()
			shortcut.name = file.name;
			if( shortcut.name.lastIndexOf( "." ) > 0 )
				shortcut.name = shortcut.name.substr( 0, shortcut.name.lastIndexOf( "." ) );
			shortcut.link = dirs[i] + "\\" + file.name;

			if( file.name.substr( file.name.lastIndexOf(".") + 1, 3 ).toLowerCase() == "lnk"  )	// Get Icon from original File
				shortcut.icon = framework.system.getFileIcon( shortcut.link )				
			else
				shortcut.icon = framework.system.getFileIcon( file.name )				

			shortcut.link = shortcut.link.replace( /\\/g, "\\\\");	
			shortcuts.push( shortcut );
		}
	}
	shortcuts.sort( sortItems );
   return;
}

/***********************************************************************
  Sort by name
***********************************************************************/
function sortItems( a, b ) {
	if( a.name > b.name ) 
		return 1;
	else if( a.name < b.name )
		return -1;
	else
		return 0;
}

